package me.luger.core.commom.util;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class ServerUtil {

	public static final String IP_TYPE_V4 = "ipv4";

	public static final String IP_TYPE_V6 = "ipv6";

	public static String getLocalIp(String ipType) {
		List<String> ipList = null;
		if (IP_TYPE_V4.equals(ipType)) {
			ipList = getLocalIp4();
		} else if (IP_TYPE_V6.equals(ipType)) {
			ipList = getLocalIp6();
		} else {
			return null;
		}
		if (ipList == null || ipList.isEmpty()) {
			return null;
		}
		return ipList.get(0);
	}

	public static List<String> getLocalIp4() {
		try {
			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
					.getNetworkInterfaces();
			if (allNetInterfaces == null) {
				return null;
			}
			List<String> ipList = new ArrayList<String>();
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = allNetInterfaces.nextElement();
				Enumeration<InetAddress> addresses = netInterface
						.getInetAddresses();
				while (addresses.hasMoreElements()) {
					InetAddress ip = addresses.nextElement();
					if (ip == null || !(ip instanceof Inet4Address)) {
						continue;
					}
					String hostAddress = ip.getHostAddress();
					if (!("127.0.0.1").equals(hostAddress)) {
						ipList.add(hostAddress);
					}
				}
			}
			return ipList;
		} catch (Exception e) {
			LogUtil.errorLog(e, null);
			return null;
		}
	}

	public static List<String> getLocalIp6() {
		try {

			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
					.getNetworkInterfaces();
			if (allNetInterfaces == null) {
				return null;
			}
			List<String> ipList = new ArrayList<String>();
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = allNetInterfaces.nextElement();
				Enumeration<InetAddress> addresses = netInterface
						.getInetAddresses();
				while (addresses.hasMoreElements()) {
					InetAddress ip = addresses.nextElement();
					if (ip == null || !(ip instanceof Inet6Address)) {
						continue;
					}
					String hostAddress = ip.getHostAddress();
					if (!("0:0:0:0:0:0:0:1").equals(hostAddress)) {
						ipList.add(hostAddress);
					}
				}
			}
			return ipList;
		} catch (Exception e) {
			LogUtil.errorLog(e, null);
			return null;
		}
	}

	public static String getLoginUser() {
		Map<String, String> map = getInfo();
		if (map == null || map.isEmpty()) {
			return null;
		} else {
			return map.get("LOGNAME");
		}
	}

	public static Map<String, String> getInfo() {
		return System.getenv();
	}
	
	public static String getHostName() {
		try {
			return InetAddress.getLocalHost().getHostName();
		} catch (Exception e) {
			LogUtil.errorLog(e, null);
			return null;
		}
	}

	public static void main(String[] args) {
		System.out.println("HostName:" + ServerUtil.getHostName());
		System.out.println(ServerUtil.getLocalIp(IP_TYPE_V4));
		Map<String, String> map = System.getenv();
		for (Entry<String, String> entry : map.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
	}

}
